
import { alertCartDB, ck, delCartDB, goodsChecked, reqCartDB } from '../api';
//home模块的小仓库
const state = {
    //state中数据默认初始值不能乱写，服务器返回对象/数组，需要根据接口返回值初始化
    cartDB: [],//首页数据

}

const actions = {
    //通过API里面的接口函数调用，向服务器发送请求，获取服务器的数据
    async getCartInfo({ commit }) {//同步
        let result = await reqCartDB();//等待请求获取结果     
        commit("GETCARTDB", result);
    },

    async delCartDB({ commit }, cartId) {
        let result = await delCartDB(cartId);
        console.log(result);
        commit('DELCARTDATA', result);
    },
    async alertCartDB({ commit },{ cartId, num, checked}) {
        console.log(cartId,
            num,
            checked);
        let result = await alertCartDB(cartId, num, checked);
        console.log(cartId);
        commit('ALERTCARTDATA', result);
    },
    async goodsCheckedAll({ commit }, checked) {
        let result = await goodsChecked(checked);
        console.log(checked);
        commit('CHECKEDALL', result);
    },
    async ckbt({ commit }, checked) {
        let result = await ck(checked);
        console.log(checked);
        commit('CK', result);
    },

}

const mutations = {
    GETCARTDB(state, cartDB) {
        state.cartDB = cartDB;
    },
    DELCARTDATA(state, cartDB) {
        state.cartDB = cartDB;
    },
    ALERTCARTDATA(state, cartDB) {
        state.cartDB = cartDB;
    },
    CHECKEDALL(state, cartDB) {
        state.cartDB = cartDB;
    },
    CK(state, cartDB) {
        state.cartDB = cartDB;
    }
}

const getters = {
    getCartDBList(state) {
        return state.cartDB;
    },
}


export default {
    state,
    actions,
    mutations,
    getters
}